package com.beamfield.common;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Hashtable;
import java.util.Properties;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QrCode {


	public static Properties properties = new Properties();

	public static Properties qrproperties = new Properties();

	static {
		try {
			String path = "config.properties";
			InputStream inStream = CommonUtils.class.getClassLoader().getResourceAsStream(path);
			if (inStream == null) {
				inStream = CommonUtils.class.getClassLoader().getResourceAsStream("/" + path);
			}
			properties.load(inStream);

			String qrpath = "qr.properties";
			InputStream qrinStream = CommonUtils.class.getClassLoader().getResourceAsStream(qrpath);
			if (qrinStream == null) {
				qrinStream = CommonUtils.class.getClassLoader().getResourceAsStream("/" + path);
			}
			qrproperties.load(qrinStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 初始化
	 * @param baseContent
	 * @param beamid
	 * @param type
	 * @param data
	 * @return
	 */
	public static String init(Long beamid,String type,String data){
		try {
			return qrproperties.getProperty("qr.api").replaceAll("\\$\\{beamid\\}",AESSecurityUtil.encrypt(beamid.toString(), AESSecurityUtil.INIT_KEY))
			.replaceAll("\\$\\{type\\}", AESSecurityUtil.encrypt(type, AESSecurityUtil.INIT_KEY))
			.replaceAll("\\$\\{data\\}", AESSecurityUtil.encrypt(data, AESSecurityUtil.INIT_KEY));
		} catch (Exception e) {
			e.printStackTrace();
			// TODO Auto-generated catch block
			return null;
		}
	}

	/**
	 * 生成二维码
	 *
	 * @return
	 * @throws Exception
	 * @throws Exception
	 */
	public static String creatQrcode(Long beamid,String type,String data) {
		try {
			String contentstr =  qrproperties.getProperty("qr.api").replaceAll("\\$\\{beamid\\}",AESSecurityUtil.encrypt(beamid.toString(), AESSecurityUtil.INIT_KEY))
					.replaceAll("\\$\\{type\\}", AESSecurityUtil.encrypt(type, AESSecurityUtil.INIT_KEY))
					.replaceAll("\\$\\{data\\}", AESSecurityUtil.encrypt(data, AESSecurityUtil.INIT_KEY));
			String path =  properties.getProperty("qrFilePath") + File.separator+ beamid+File.separator + type + File.separator;
			CommonUtils.checkPath(path);
			String servicename = CommonUtils.getTimeFormat(new Date(), "yyyyMMddhhmmssSSS") + "_" + (int) (Math.random() * 100) + ".png";
			String content = new String(contentstr.getBytes("UTF-8"), "ISO-8859-1");
			Hashtable<EncodeHintType, Object> hintMap = new Hashtable<EncodeHintType, Object>();
			hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
			QRCodeWriter qrCodeWriter = new QRCodeWriter();
			BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 800, 800, hintMap);
			int CrunchifyWidth = byteMatrix.getWidth();
			BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
			image.createGraphics();
			Graphics2D graphics = (Graphics2D) image.getGraphics();
			graphics.setColor(Color.WHITE);
			graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
			graphics.setColor(Color.BLACK);
			for (int i = 0; i < CrunchifyWidth; i++) {
				for (int j = 0; j < CrunchifyWidth; j++) {
					if (byteMatrix.get(i, j)) {
						graphics.fillRect(i, j, 1, 1);
					}
				}
			}
			File file1 = new File(path, servicename);
			// 创键编码器，用于编码内存中的图象数据。
			ImageIO.write(image, "png", file1);
			return  servicename;
		} catch (WriterException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	

}
